//
//  ZCSamplingDetailVC.m
//  zctx
//
//  Created by apple on 15/10/12.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCSamplingDetailVC.h"
#import "ZCSamplingProgressCell.h"

#import "ZCRaisePayVC.h"

#import "ZCMechanismVC.h"
#import "ZCHarmprofileVC.h"
#import "ActionView.h"


@interface ZCSamplingDetailVC ()<UMSocialUIDelegate>

@end

@implementation ZCSamplingDetailVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCSamplingDetailVC"];
    [MTA trackPageViewBegin:@"ZCSamplingDetailVC"];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCSamplingDetailVC"];
    [MTA trackPageViewEnd:@"ZCSamplingDetailVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"检测项目详情";
    
    self.secondArray = [NSArray arrayWithObjects:@"产品名称:",@"品牌:",@"分类:",@"抽样地:",@"检测机构:",nil];
    NSLog(@"pid = %@",self.pid);
    infoArray = [NSArray array];
    datadictionary = [NSMutableDictionary dictionaryWithCapacity:0];
    sponsorArray = [NSMutableArray arrayWithCapacity:0];
    
    [self initnavegation]; //导航栏
    [self requestProjectdetailsdata];//请求项目详情数据
    [self requestSupporterdata]; //请求支持者数据
    
    [self initTableView];
    [self initTableViewHeaderView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInvolvedDeatailsProgressCell" bundle:nil] forCellReuseIdentifier:@"involvedprogressCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCProjectdetailsSponsorCell" bundle:nil] forCellReuseIdentifier:@"sponsorcell"];

}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 24, 22);
    [rightButton setImage:[UIImage imageNamed:@"main_share"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
    
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark 请求项目详情数据
-(void)requestProjectdetailsdata{
    ProgressHUDStr;
    
    NSLog(@"ssspid = %@",_pid);
    [NetworkManager requestFoundingGetOneWithId:_pid Mid:UserModel.mid password:UserModel.password success:^(NSDictionary *dictResponse) {
        ProgressDismiss;
        datadictionary = [dictResponse mutableCopy];
        
        NSLog(@"ssscount  = %ld",(unsigned long)[datadictionary count]);
        self.titlenameLable.text = [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"pjname"]];
        
        NSString *pdname =[NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"pdname"]]; //名称
        NSString *brand = [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"brand"]]; //品牌
        NSString *packageid = [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"category"]];  //分类
        NSString *location = [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"location"]];  //抽样地
        NSString *institution = [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"institution"]];  //检测机构
        
        infoArray = @[pdname,brand,packageid,location,institution];
        NSLog(@"infoArray = %@",infoArray);
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        ProgressDismiss;
        [self.tableView removeFromSuperview];
    }];
}



#pragma mark 请求支持者数据
-(void)requestSupporterdata{
    ProgressHUDStr;
    [NetworkManager requestFoundingGetOneSponsorWithId:self.pid Mid:UserModel.mid password:UserModel.password pagecount:@"0" success:^(NSDictionary *dictResponse) {
        ProgressDismiss;
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            NSArray *array = (NSArray *)[dictResponse objectForKey:@"content"];
            sponsorArray = [array mutableCopy];
            NSIndexSet *index = [NSIndexSet indexSetWithIndex:4];
            [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];
        }
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
}

#pragma mark TableViewHeaderView

-(void)initTableViewHeaderView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,44)];
    headView.backgroundColor = [UIColor whiteColor];
    
    self.titlenameLable = [UILabel new];
    [headView addSubview:self.titlenameLable];
    self.titlenameLable.font = [UIFont systemFontOfSize:16];
    self.titlenameLable.textAlignment = NSTextAlignmentCenter;
    [self.titlenameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headView.mas_centerX);
        make.centerY.equalTo(headView.mas_centerY);
        make.width.mas_equalTo(@(SCREEN_WIDTH));
        make.height.mas_equalTo(@(30));
    }];
    self.tableView.tableHeaderView = headView;
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==4) {
        return sponsorArray.count;
    }else{
        if (section==0) {
            return 1;
        }else if(section==1){
            return 2;
        }else if(section==2){
            return 5;
        }else{
            return 1;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCDeatailsHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headercell"];
    if (indexPath.section==0) {
        cell = [[ZCDeatailsHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headercell" cellType:ZCDeatailsHeaderCellTypestate];
        cell.labelTitle.text =@"检测状态:";
        cell.stateImgView.image = [UIImage imageNamed:@"details_sampling"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section==1) {
        if (indexPath.row==0) {
            cell = [[ZCDeatailsHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headercell" cellType:ZCDeatailsHeaderCellTypedetails];
            cell.labelTitle.text = @"检测内容:";
            cell.detailsView.titleLable.text = @"危害简介";
            cell.detailsView.titleColor = [UIColor redColor];
        }else{
            cell = [[ZCDeatailsHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headercell" cellType:ZCDeatailsHeaderCellTypecontent];
            if (datadictionary.count>0) {
                cell.labelcontent.text = [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"packagename"]];
            }else{
                cell.labelcontent.text =@"";
            }
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section==2){
        
        cell = [[ZCDeatailsHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headercell" cellType:ZCDeatailsHeaderCellTypeinfo];
        cell.labelTitle.text = self.secondArray[indexPath.row];
        if (infoArray.count>0) {
            cell.labelInfo.text = infoArray[indexPath.row];
        }
        if (indexPath.row==4) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.section==3){
        ZCDeatailsHeaderCell *cell = [[ZCDeatailsHeaderCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"headercell" cellType:ZCDeatailsHeaderCellTypeinfo];
        cell.labelTitle.text = @"目标金额:";
        float money =[[NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"emoney"]]floatValue]/100;
        cell.labelInfo.text = [NSString stringWithFormat:@"%.0f元",money];
        cell.labelInfo.textColor = [UIColor redColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        ZCProjectdetailsSponsorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sponsorcell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 60;
    }else if (indexPath.section==1) {
        if (indexPath.row==0) {
            return 44;
        }else{
            NSString *content =  [NSString stringWithFormat:@"%@",[datadictionary objectForKey:@"packagename"]];
            CGRect rect= [content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]} context:nil];
            return ((rect.size.height+15)>44)?(rect.size.height+15):44;
        }
    }else if (indexPath.section==2){
        return 44;
    }else if(indexPath.section==3){
        return 44;
    }else{
        return 60;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section<3) {
        return 10;
    }else{
        return 40;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section<3) {
        UIView * headView = [[UIView alloc]init];
        return headView;
    }else{
        NSArray *titleArray = [NSArray arrayWithObjects:@"众测信息",@"支持者",nil];
        UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        
        UIView *newView = [UIView new];
        [headView addSubview:newView];
        newView.backgroundColor = [UIColor whiteColor];
        [newView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headView.top).with.offset(10);
            make.left.equalTo(headView).with.offset(0);
            make.right.equalTo(headView).with.offset(0);
            make.bottom.equalTo(headView).with.offset(0);
        }];
        
        UILabel *leftlable = [UILabel new];
        [newView addSubview:leftlable];
        leftlable.font = [UIFont systemFontOfSize:16];
        [leftlable setText:titleArray[section-3]];
        [leftlable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(newView.mas_centerY);
            make.left.equalTo(newView).with.offset(15);
            make.height.mas_equalTo(@30);
            make.width.mas_equalTo(@150);
        }];
        
        if (section==3) {
            UILabel *statelable = [UILabel new];
            [newView addSubview:statelable];
            statelable.font = [UIFont systemFontOfSize:16];
            statelable.textColor = MainColor;
            [statelable setText:[NSString stringWithFormat:@"已完成众测"]];
            [statelable mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(newView.mas_centerY);
                make.right.equalTo(newView).with.offset(-15);
                make.height.mas_equalTo(@30);
            }];
            
        }
        return headView;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1 && indexPath.row==0) {
        ZCHarmprofileVC *vc = [[ZCHarmprofileVC alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.pid = self.pid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section==2 && indexPath.row==4) {//机构简介
        ZCMechanismVC *vc = [[ZCMechanismVC alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.pid = self.pid;
        [self.navigationController pushViewController:vc animated:YES];
    }

}


#pragma mark 分享点击
-(void)shareButtonPressed:(id *)button{
    ActionView *action = [[ActionView alloc]initWithFrame:CGRectZero];
    action.pid = self.pid;
    [action showInView:self.view];
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
